// var fs = require('fs');3
// var request = require("request");
// var src = "http://n.sinaimg.cn/news/transform/20170211/F57R-fyamvns4810245.jpg";
// var writeStream = fs.createWriteStream('image.png');var readStream = request(src)
// readStream.pipe(writeStream);
// readStream.on('end', function() {    console.log('文件下载成功');
// });
// readStream.on('error', function() {    console.log("错误信息:" + err)
// })
// writeStream.on("finish", function() {    console.log("文件写入成功");
//     writeStream.end();
// });

var http = require('http')
var fs = require('fs');
 
// 要抓取的网页地址
var url = 'http://n.sinaimg.cn/news/transform/20170211/F57R-fyamvns4810245.jpg'
 
http.get(url, function(res) {
	var a = ''
    res.setEncoding("binary");
	res.on('data', function(data) {
		a += data;
	})
	res.on('end', function() {
		fs.writeFile('doodle.png', a,'binary', function(err) {
			if (err) {
				console.log('出现错误!')
			}
			console.log('已输出至doodle.png中')
		})
	})
}).on('error', function(err) {
	console.log('错误信息：' + err)
})
// var request=require('request');
// request('http://n.sinaimg.cn/news/transform/20170211/F57R-fyamvns4810245.jpg').pipe(fs.createWriteStream('doodle.png'));